package com.gxa.insurance.controller;


import com.gxa.insurance.pojo.Customer;
import com.gxa.insurance.service.CustomerService;
import com.gxa.insurance.service.OrderService;
import com.gxa.insurance.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CustomerController
 * @Description 客户接口
 * @Author ds
 * @Date 2022/6/6 16:25
 * @Version 1.0
 */
@Api
@RestController
@RequestMapping("/customer")
@CrossOrigin
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @Autowired
    OrderService orderService;

    @ApiOperation(value = "添加用户方法",produces = "body")
    @PostMapping("/add")
    public Map add(@RequestBody Customer customer) throws Exception {

        customerService.add(customer);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(customer);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "修改用户方法",produces = "body")
    @PostMapping("/update")
    public Map update(@RequestBody Customer customer) throws Exception {

        customerService.update(customer);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "查询所用用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "条数",defaultValue = "10",dataType = "int",paramType = "query")
    })
    @PostMapping("/selectByPage")
    public Map selectByPage(@RequestParam("page") Integer page,@RequestParam("size") Integer size) throws Exception {
        Map<String, Object> map = customerService.selectByPage(page, size);
        Object count = map.get("count");
        Object customers = map.get("customers");
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("count",count);
        jsonResult.setData(customers);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "根据手机号或姓名查询",produces = "body")
    @ApiImplicitParam(name = "(不传然和条件就是查询所有)查询条件字段  和分页数据  以json方式传入",value="传值示例，请保持字段名相同 " +
            "{\n" +
            "    \"name\":\"姓名或手机号\",\n" +
            "    \"page\":\"1\",\n" +
            "    \"size\":\"10\",\n" +
            "}\n")
    @PostMapping("/selectByName")
    public Map selectLikeName(@RequestBody Map<String,String> map) throws Exception {

        Map<String, Object> ListMap = customerService.selectLikeName(map);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("count",ListMap.get("count"));
        jsonResult.setData(ListMap.get("customers"));
        return jsonResult.getMap();
    }

    @ApiOperation(value = "根据id进行查询(可配合删除使用)")
    @ApiImplicitParam(name = "row",value = "编号",dataType = "int",paramType = "query")
    @PostMapping("/selectById")
    public Map selectById(@RequestParam("row") Integer id) throws Exception {

        Customer customers = customerService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(customers);
        return jsonResult.getMap();
    }

    @ApiOperation("查询所有用户的邮箱")
    @PostMapping("/selectEmail")
    public Map selectEmail() throws Exception {
        List<Customer> customers = customerService.selectEmail();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(customers);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "查询用户的邮箱是否存在(兑换码)")
    @ApiImplicitParam(name = "email",value = "用户的邮箱",paramType = "query")
    @PostMapping("/selectByEmailAndCoupon")
    public Map selectByEmailAndCoupon(String email) throws Exception {
        JsonResult jsonResult = new JsonResult();
        Map<String, Object> map = customerService.selectByEmailAndCoupon(email);
        jsonResult.setCode("1");
        jsonResult.setMsg("用户查询成功");
        jsonResult.setData(map);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "查询用户的邮箱是否存在")
    @ApiImplicitParam(name = "email",value = "用户的邮箱",paramType = "query")
    @PostMapping("/selectByEmail")
    public Map selectByEmail(String email) throws Exception {
        boolean boo = customerService.selectByEmail(email);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(boo);
        return jsonResult.getMap();
    }
}

